package com.aegean.CommercialHotelBookSystem.user.business.Impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;

import com.aegean.CommercialHotelBookSystem.factory.DaoFactory;
import com.aegean.CommercialHotelBookSystem.model.Book;
import com.aegean.CommercialHotelBookSystem.model.BookDetail;
import com.aegean.CommercialHotelBookSystem.model.Customer;
import com.aegean.CommercialHotelBookSystem.model.component.Photo;
import com.aegean.CommercialHotelBookSystem.user.business.IUserBusiness;
import com.aegean.CommercialHotelBookSystem.user.dao.IUserDao;
import com.aegean.CommercialHotelBookSystem.user.dao.Impl.UserDaoImpl;

public class UserBusinessImpl implements IUserBusiness {

	@Override
	public boolean addUser(String dir, String picName, String fileName,
			String fileType, String User_Name, String User_RealName,
			String User_Pwd, String User_Sex, String User_Tel,
			String User_Email, String User_Identity) {
		Customer one = new Customer();
		Photo photo = new Photo();
		try {
			photo.setPhoto_Name(fileName);
			photo.setPhoto_Type(fileType);
			File User_Photo = new File(dir + "/upload/User/" + picName);
			InputStream in = new FileInputStream(User_Photo);
			photo.setPhoto(Hibernate.createBlob(in));
			one.setPerson_Login(User_Name);
			one.setPerson_Name(User_RealName);
			one.setPerson_Password(User_Pwd);
			one.setPerson_Sex(User_Sex.charAt(0));
			one.setPerson_Photo(photo);
			one.setCustomer_State(1);
			one.setCustomer_Phone(User_Tel);
			one.setCustomer_Email(User_Email);
			one.setCustomer_IdentityCard(User_Identity);
			IUserDao userdao =DaoFactory.createUserDao();
			userdao.saveObject(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean editUser(Customer one, String dir,
			String fileName, String fileType, String User_Name,
			String User_RealName, String User_Pwd, String User_Sex,
			String User_Tel, String User_Email, String User_Identity) {
		try {
			if (fileName != null) {
				Photo photo = new Photo();
				photo.setPhoto_Name(fileName);
				photo.setPhoto_Type(fileType);
				File User_Photo = new File(dir + "/upload/User/" + fileName);
				InputStream in = new FileInputStream(User_Photo);
				photo.setPhoto(Hibernate.createBlob(in));
				one.setPerson_Photo(photo);
			}
			one.setPerson_Login(User_Name);
			one.setPerson_Name(User_RealName);
			one.setPerson_Sex(User_Sex.charAt(0));
			one.setCustomer_Phone(User_Tel);
			one.setCustomer_Email(User_Email);
			one.setCustomer_IdentityCard(User_Identity);
			IUserDao userdao =DaoFactory.createUserDao();
			userdao.updateObject(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean editPwd(Customer one, String newPwd) {
		try {
			one.setPerson_Password(newPwd);
			IUserDao userdao =DaoFactory.createUserDao();
		    userdao.updatePwd(one);
		    return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public Customer loginUser(String User_Login, String User_Pwd) {
		IUserDao userdao =DaoFactory.createUserDao();
		Customer one = userdao.searchCustomer(User_Login, User_Pwd);
		return one;
	}
	@Override
	public List<Book> searchBooks(Customer customer,String hotel_Name,String book_Date) {
		IUserDao userdao =DaoFactory.createUserDao();
		return userdao.searchBooks(customer,hotel_Name,book_Date);
	}
	@Override
	public List<BookDetail> searchBookDetails(int book_Id) {
		IUserDao userdao =DaoFactory.createUserDao();
		return userdao.searchBookDetails(book_Id);
	}
	@Override
	public boolean serachUser(String User_Name) {
		IUserDao userdao =DaoFactory.createUserDao();
		return userdao.serachUser(User_Name);
	}

}
